﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PPI.Client.Logic;
using PPI.Client.Utils;
using PPI.Entities;
using PPI.Entities.Enumerator;
using PPI.Entities.XML;
using PPI.General;

namespace PPI.Client.Presenter
{
    public interface IVisitCrud
    {
        int ProgressBarValue { set; }
        string ErrorMessage { set; }
        EVendor_Visit_XMissionStructure Visit { get; }
        List<EVendor_Visit_XMissionStructures_Image> DeleteImages { get; }
        List<EVendor_Visit_XMissionStructures_Pole> DeletePoles { get; }
        string ConnectionString{ get;}
        EXmlOutboxBase Database{ get;}
        bool ShowImages{ get; set;}
    }

    public class VisitCrudPresenter
    {
        private IVisitCrud m_View;

        public VisitCrudPresenter(IVisitCrud view)
        {
            m_View = view;
        }

        public void SaveVisit()
        {
            try
            {
                switch (m_View.Database.DatabaseType)
                {
                    case DatabaseTypes.GPS:
                        saveMobileVisit(m_View.ConnectionString);
                        break;
                    case DatabaseTypes.Project:
                        saveProjectVisit();
                        DeleteProjetVisit(m_View.Database.ConnectionString);
                        saveProjectVisit(m_View.Database.ConnectionString);
                        break;
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
                throw  ex;
            }
        }

        private void saveMobileVisit(string connectionString)
        {
            LVendor_Visit_XMissionStructure.SaveMobile(m_View.Visit,connectionString);

            foreach (EVendor_Visit_XMissionStructures_Pole p in m_View.DeletePoles)
            {

                LVendor_Visit_XMissionStructures_Pole.DeleteMobile(p, connectionString);
            }

            foreach (EVendor_Visit_XMissionStructures_Image i in m_View.DeleteImages)
            {

                LVendor_Visit_XMissionStructures_Image.DeleteMobile(i, connectionString);
            }            
        }

        private void saveProjectVisit(string connectionString)
        {
            LVendor_Visit_XMissionStructure.InsertMobileProject(m_View.Visit,m_View.ShowImages, connectionString);

            foreach (EVendor_Visit_XMissionStructures_Pole p in m_View.DeletePoles)
            {
                LVendor_Visit_XMissionStructures_Pole.DeleteMobile(p, connectionString);
            }

            foreach (EVendor_Visit_XMissionStructures_Image i in m_View.DeleteImages)
            {

                LVendor_Visit_XMissionStructures_Image.DeleteMobile(i, connectionString);
            }
        }

        private void saveProjectVisit()
        {
            LVendor_Visit_XMissionStructure.Save(m_View.Visit);

            foreach (EVendor_Visit_XMissionStructures_Pole p in m_View.Visit.Poles)
            {
                LVendor_Visit_XMissionStructures_Pole.Save(p);
            }

            if(m_View.ShowImages)
            {
                WebUploaderHelper _UploaderHelper = new WebUploaderHelper(Constants.K_WEBSITEUPLOADER);
                foreach (EVendor_Visit_XMissionStructures_Image i in m_View.Visit.Images)
                {
                    bool isToUpload = false;
                    if (i.Id == Int32.MinValue)
                        isToUpload = true;
                    i.Pole.ServerID = i.Pole.Id;
                    i.Asset.ServerID = i.Asset.Id;
                    LVendor_Visit_XMissionStructures_Image.Save(i);
                    if (isToUpload)
                    {
                        string _FileName = i.Asset.Id + "_" + i.Id + ".jpg";
                        string _FolderName = DirHelper.GetImageDirectory(i.Asset.Id, i.Id);

                        if (i.ImageFile.Exists)
                            _UploaderHelper.Upload(_FolderName, _FileName, i.ImageFile, Enum.GetName(typeof(DatabaseTypes), DatabaseTypes.GPS));

                    }

                }
            }


            foreach (EVendor_Visit_XMissionStructures_Pole p in m_View.DeletePoles)
            {
                LVendor_Visit_XMissionStructures_Pole.Delete(p);
            }

            if(m_View.ShowImages)
            {
                foreach (EVendor_Visit_XMissionStructures_Image i in m_View.DeleteImages)
                {
                    LVendor_Visit_XMissionStructures_Image.Delete(i);
                }
            }

        }

        private void DeleteProjetVisit(string connectionString)
        {
            LVendor_Visit_XMissionStructure.DeleteProject(m_View.Visit,connectionString);
        }
    }
}
